package cn.devices.service.impl;

import cn.devices.dto.SelectBorrowDto;
import cn.devices.dto.UserBorrowDto;
import cn.devices.entity.TbBorrow;
import cn.devices.mapper.TbBorrowMapper;
import cn.devices.service.TbBorrowService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * <p>
 * 设备出借表 服务实现类
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@Service
public class TbBorrowServiceImpl implements TbBorrowService {

    @Autowired
    private TbBorrowMapper tbBorrowMapper;


    @Override
    public String borrow(String deviceCode) {
        return tbBorrowMapper.borrow(deviceCode);
    }

    @Override
    public int userCount(String userId) {
        return tbBorrowMapper.userCount(userId);
    }

    @Override
    public int count(String deviceCode) {
        return tbBorrowMapper.count(deviceCode);
    }

    @Override
    public UserBorrowDto userBorrowDtoDm(String userId, String deviceCode) {
        return tbBorrowMapper.userBorrowDtoDm(userId, deviceCode);
    }

    @Override
    public UserBorrowDto userBorrowDtoSelf(String userId, String deviceCode) {
        return tbBorrowMapper.userBorrowDtoSelf(userId, deviceCode);
    }

    @Override
    public TbBorrow tbBorrowSelect(String deviceCode) {
        return tbBorrowMapper.tbBorrowSelect(deviceCode, "");
    }

    @Override
    public PageInfo<SelectBorrowDto> BorrowDtoList(int pageIndex, int pageSize, String beginTime, String endTime, String userName, String telephone, String deviceCode, String deviceName, String typeId) {
        PageHelper.startPage(pageIndex, pageSize);
        List<SelectBorrowDto> borrowDtoList = tbBorrowMapper.BorrowDtoList(beginTime, endTime, userName, telephone, deviceCode, deviceName, typeId);
        for (SelectBorrowDto dto:borrowDtoList
             ) {
            File file = new File(dto.getBorrowImage());
            if (file.exists()) {
                try {
                    dto.setImgb(FileUtils.readFileToByteArray(file));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();

                }
            }
            File files = new File(dto.getRetImage());
            if (files.exists()) {
                try {
                    dto.setImgr(FileUtils.readFileToByteArray(files));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();

                }
            }
        }
        return new PageInfo<SelectBorrowDto>(borrowDtoList);
    }

    @Override
    public int tbBorrowAdd(TbBorrow tbBorrow) {
        return tbBorrowMapper.tbBorrowAdd(tbBorrow);
    }

    @Override
    public String img(String userId) {
        return tbBorrowMapper.img(userId);
    }

    @Override
    public int isBorrow(String isBorrow, String deviceCode,String deviceStatus) {
        return tbBorrowMapper.isBorrow(isBorrow, deviceCode,deviceStatus);
    }

    @Override
    public int tbBorrowUpdate(TbBorrow tbBorrow) {
        return tbBorrowMapper.tbBorrowUpdate(tbBorrow);
    }

    @Override
    public List<TbBorrow> getUserIdBorrow(String userId) {
        return tbBorrowMapper.getUserIdBorrow(userId);
    }


}
